
package com.scs.application.modules.upms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.upms.entity.Menu;
import com.scs.application.modules.upms.entity.Shortcut;
import com.scs.application.modules.upms.mapper.ShortcutMapper;
import com.scs.application.modules.upms.request.ShortcutQueryRequest;
import com.scs.application.modules.upms.service.MenuService;
import com.scs.application.modules.upms.service.ShortcutService;
import com.scs.application.modules.upms.vo.UserShortcutVo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 快捷方式 service 实现
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ShortcutServiceImpl extends BaseServiceImpl<ShortcutMapper, Shortcut> implements ShortcutService {

    @Autowired
    private ShortcutMapper shortcutMapper;

    @Autowired
    private MenuService menuService;

    @Override
    public List<UserShortcutVo> list(ShortcutQueryRequest request) {

        List<UserShortcutVo> shortcutVoList = shortcutMapper.listShortCut(request.getUserId());
        return shortcutVoList;
    }

    @Override
    public IPage<Shortcut> page(ShortcutQueryRequest request) {
        IPage page = this.page(request.getPage(), Wrappers.<Shortcut>query()
        );
        return page;
    }

    @Override
    public boolean batchSaveShortcut(String[] menuIds) {
        String userId = UserUtils.currentUser().getId();
        List<Shortcut> shortcutList = new ArrayList<>();
        shortcutMapper.delete(Wrappers.<Shortcut>query().eq("user_id", userId));
        for (String menuId : menuIds) {
            Shortcut shortcut = new Shortcut();
            shortcut.setUserId(userId);
            shortcut.setMenuId(menuId);
            shortcutList.add(shortcut);
        }

        return this.saveBatch(shortcutList);
    }

    @Override
    public List<Shortcut> batchSaveMenu(List<Shortcut> list) {
        String userId = UserUtils.currentUser().getId();
        List<Shortcut> shortcutList = new ArrayList<>();
        shortcutMapper.delete(Wrappers.<Shortcut>query().eq("user_id", userId));
        for (Shortcut shortcut : list) {
            shortcut.setUserId(userId);
            shortcut.setMenuId(shortcut.getMenuId());
            shortcut.setSortNum(shortcut.getSortNum());
            shortcutList.add(shortcut);
        }
        this.saveBatch(shortcutList);
        return shortcutList;
    }
}
